<template>
  <div class="login_contianer">
    <div class="login_form">
      <div class="logo">
        <div class="logo_icon">
          <img src="../../assets/image/icon/logo_index.png" alt="" />
        </div>
      </div>

      <div class="box_form">
        <el-form class="login-form" ref="form" :model="user" :rules="rules">
          <el-form-item prop="mobile">
            <el-input
              v-model="user.mobile"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input v-model="user.code" placeholder="请输入验证码"></el-input>
          </el-form-item>
          <el-form-item prop="agree">
            <el-checkbox v-model="user.agree" 
              >我已阅读并同意用户协议和隐私条款</el-checkbox
            >
          </el-form-item>
          <el-form-item >
            <el-button
              class="login_btn"
              type="primary"
              @click="getLogin"
              :loading="isLoading"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { onLogin } from "@/request/api.js";
import { mapMutations, mapState } from "vuex";
export default {
  name: "MyLogin",
  data() {
    return {
      user: {
        mobile: "", // 手机号
        code: "", // 验证码
        agree: false,// 协议
      },
      
      isLoading: false,
       // 是否同意协议的选中状态

      rules: {
        mobile: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: "请输入正确的号码格式",
            trigger: "change",
          },
        ],
        code: [
          { required: true, message: "验证码不能为空", trigger: "change" },
          { pattern: /^\d{6}$/, message: "请输入正确的验证码格式" },
        ],
        // 自定校验规则
        agree:[
            {
                validator:(rule,value,callback)=>{
                   if(value){
                       callback()
                   }else{
                       callback(new Error('请同意用户协议'))
                   }
                },
                
               trigger: "change"
            }
        ]
      },
    };
  },
  computed: {
    ...mapState("user", ["token"]),
  },
  methods: {
    ...mapMutations("user", ["saveToken"]),
    //   登录函数
    getLogin() {
      this.$refs["form"].validate((valid,err) => {
        if (!valid) {
            console.log(err)
          return;
        } else {
          this.login()
        }
      });
    },

    // 登录请求
    login(){
        this.isLoading = true;

          onLogin(this.user)
            .then((res) => {
              console.log("请求成功", res.data);

              this.$message({
                message: "登录成功",
                type: "success",
              });

              window.localStorage.setItem('user',JSON.stringify(res.data.data))

                // 跳转首页
              this.$router.push('/')

              this.isLoading = false;
            })
            .catch((err) => {
              console.log("登录失败", err);
              this.$message.error("登录失败");
              this.isLoading = false;
            });
    },

    // 收起打开侧边导航
    showAside(){

    }
  },
};
</script>

<style lang="less" scoped>
.login_contianer {
  width: 100%;
  height: 100vh;
  background: url(../../assets/image/icon/login_bg.jpg) no-repeat;
  background-size: cover;
  position: relative;
  .login_form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    z-index: 1;
    padding: 30px 40px 20px 40px;
    min-width: 400px;
    border-radius: 4px;
    .logo {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 20px;

      .logo_icon {
        width: 200px;
        > img {
          width: 100%;
          display: block;
        }
      }
    }
    .box_form {
      .login_btn {
        width: 100%;
       
      }
    }
  }
}
</style>
